/*
 * PanelLaplacian.java
 *
 * Created on 2008年4月10日, 下午4:10
 */

package GUI;

import Manager.Manager;
import Global.*;
/**
 *
 * @author  littlechild
 */
public class PanelLaplacian extends javax.swing.JPanel {
    Manager manager;
    PanelCavas drawcavas;
    boolean isInitiating;
    static final int maxSlidePos=10; //actually size is 6*2+1
    static final int minSlidePos=3;
    static final int maxSlideNegative=2;
    static final int minSlideNegative=0;
    static final int maxSlideStrict=10;
    static final int minSlideStrict=0;
    /** Creates new form PanelLaplacian */
    public PanelLaplacian(Manager mg,PanelCavas pc) {
        manager=mg;
        drawcavas=pc;
        initComponents();
        init();
    }

    private void init() {
        this.isInitiating=true;
        this.jSliderPositive.setMaximum(maxSlidePos);
        this.jSliderPositive.setMinimum(minSlidePos);
        this.jSliderPositive.setValue(maxSlidePos-1);
        this.jSliderNegative.setMaximum(maxSlideNegative);
        this.jSliderNegative.setMinimum(minSlideNegative);
        this.jSliderNegative.setValue(maxSlideNegative-1);
        this.jSliderStrict.setMaximum(maxSlideStrict);
        this.jSliderStrict.setMinimum(minSlideStrict);
        this.jSliderStrict.setValue(maxSlideStrict-1);
        this.isInitiating=false;
    }
    private void preview(boolean isStrict) {
        if(isInitiating)
            return;
        //this.jLabel4.setText("processing...");
        //this.jLabel6.setText("processing...");
        String paras[]=null;
        if(isStrict) {
            paras=new String [] {
              String.valueOf(sliderValue2realValue(this.jSliderStrict.getValue()))
            };
        }else {
            paras=new String [] {
              String.valueOf(sliderValue2realValue(this.jSliderPositive.getValue())),
              String.valueOf(sliderValue2realValue(this.jSliderNegative.getValue()))
            };
        }
        this.drawcavas.setImage(
                manager.operate(Global.opLaplacian, paras,true));
        //this.jLabel4.setText("OK");
        //this.jLabel6.setText("OK");
        javax.swing.JOptionPane.showMessageDialog(this, "操作完成","操作完成",javax.swing.JOptionPane.INFORMATION_MESSAGE);
    }
    
    private int sliderValue2realValue(int sv) {
        return 2*sv+1;
    }
    private int realValue2sliderValue(int rv)  {
        return rv/2;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelAppro = new javax.swing.JPanel();
        jSliderPositive = new javax.swing.JSlider();
        jSliderNegative = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabelPositive = new javax.swing.JLabel();
        jLabelNegative = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanelStrict = new javax.swing.JPanel();
        jSliderStrict = new javax.swing.JSlider();
        jLabel5 = new javax.swing.JLabel();
        jLabelStrict = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jLabel3.setText("jLabel3");

        jSliderPositive.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderPositiveMouseReleased(evt);
            }
        });
        jSliderPositive.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderPositiveStateChanged(evt);
            }
        });

        jSliderNegative.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderNegativeMouseReleased(evt);
            }
        });
        jSliderNegative.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderNegativeStateChanged(evt);
            }
        });

        jLabel1.setText("高斯半径正：");

        jLabel2.setText("高斯半径负：");

        javax.swing.GroupLayout jPanelApproLayout = new javax.swing.GroupLayout(jPanelAppro);
        jPanelAppro.setLayout(jPanelApproLayout);
        jPanelApproLayout.setHorizontalGroup(
            jPanelApproLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelApproLayout.createSequentialGroup()
                .addGroup(jPanelApproLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelApproLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanelApproLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSliderPositive, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSliderNegative, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelApproLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelApproLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelApproLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelNegative)
                            .addComponent(jLabelPositive))))
                .addContainerGap(73, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelApproLayout.createSequentialGroup()
                .addContainerGap(273, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addContainerGap())
        );
        jPanelApproLayout.setVerticalGroup(
            jPanelApproLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelApproLayout.createSequentialGroup()
                .addComponent(jLabel4)
                .addGap(11, 11, 11)
                .addGroup(jPanelApproLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabelPositive))
                .addGap(18, 18, 18)
                .addComponent(jSliderPositive, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(jPanelApproLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabelNegative))
                .addGap(18, 18, 18)
                .addComponent(jSliderNegative, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(88, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("近似方式", jPanelAppro);

        jSliderStrict.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderStrictMouseReleased(evt);
            }
        });
        jSliderStrict.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderStrictStateChanged(evt);
            }
        });

        jLabel5.setText("高斯半径：");

        javax.swing.GroupLayout jPanelStrictLayout = new javax.swing.GroupLayout(jPanelStrict);
        jPanelStrict.setLayout(jPanelStrictLayout);
        jPanelStrictLayout.setHorizontalGroup(
            jPanelStrictLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelStrictLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelStrictLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSliderStrict, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelStrictLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabelStrict)))
                .addContainerGap(73, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelStrictLayout.createSequentialGroup()
                .addContainerGap(273, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addContainerGap())
        );
        jPanelStrictLayout.setVerticalGroup(
            jPanelStrictLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelStrictLayout.createSequentialGroup()
                .addComponent(jLabel6)
                .addGap(52, 52, 52)
                .addGroup(jPanelStrictLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabelStrict))
                .addGap(18, 18, 18)
                .addComponent(jSliderStrict, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(129, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("严格方式", jPanelStrict);

        jButton1.setText("确定");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("取消");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(80, 80, 80)
                        .addComponent(jButton2))
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(12, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String paras[]=null;
        if(this.jTabbedPane1.getSelectedComponent()==this.jPanelStrict) {
            paras=new String [] {
              String.valueOf(sliderValue2realValue(this.jSliderStrict.getValue()))
            };
        }else {
            paras=new String [] {
              String.valueOf(sliderValue2realValue(this.jSliderPositive.getValue())),
              String.valueOf(sliderValue2realValue(this.jSliderNegative.getValue()))
            };
        }
        this.drawcavas.setImage(
                manager.operate(Global.opLaplacian, paras));
        CommonFunc.disposeFrame(this);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.drawcavas.setImage(
                manager.operate(Global.opNone, null));
        CommonFunc.disposeFrame(this);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jSliderStrictStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderStrictStateChanged
        // TODO add your handling code here:
        this.jLabelStrict.setText(String.valueOf(
                sliderValue2realValue(this.jSliderStrict.getValue())));
    }//GEN-LAST:event_jSliderStrictStateChanged

    private void jSliderStrictMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderStrictMouseReleased
        // TODO add your handling code here:
        preview(true);
    }//GEN-LAST:event_jSliderStrictMouseReleased

    private void jSliderPositiveStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderPositiveStateChanged
        // TODO add your handling code here:
        this.jLabelPositive.setText(String.valueOf(
                sliderValue2realValue(this.jSliderPositive.getValue())));
    }//GEN-LAST:event_jSliderPositiveStateChanged

    private void jSliderPositiveMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderPositiveMouseReleased
        // TODO add your handling code here:
        this.jSliderNegative.setMaximum(this.jSliderPositive.getValue());
        preview(false);
    }//GEN-LAST:event_jSliderPositiveMouseReleased

    private void jSliderNegativeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderNegativeStateChanged
        // TODO add your handling code here:
        this.jLabelNegative.setText(String.valueOf(
                sliderValue2realValue(this.jSliderNegative.getValue())));
    }//GEN-LAST:event_jSliderNegativeStateChanged

    private void jSliderNegativeMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderNegativeMouseReleased
        // TODO add your handling code here:
        this.jSliderPositive.setMinimum(this.jSliderNegative.getValue());
        preview(false);
    }//GEN-LAST:event_jSliderNegativeMouseReleased
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabelNegative;
    private javax.swing.JLabel jLabelPositive;
    private javax.swing.JLabel jLabelStrict;
    private javax.swing.JPanel jPanelAppro;
    private javax.swing.JPanel jPanelStrict;
    private javax.swing.JSlider jSliderNegative;
    private javax.swing.JSlider jSliderPositive;
    private javax.swing.JSlider jSliderStrict;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    
}
